package com.gangquan360.smartadmin.module.order.saleorder.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.order.saleorder.dao.SaleOrderProductDao;
import com.gangquan360.smartadmin.module.order.saleorder.domain.dto.SaleOrderProductDTO;
import com.gangquan360.smartadmin.module.order.saleorder.domain.dto.SaleOrderProductQueryDTO;
import com.gangquan360.smartadmin.module.order.saleorder.domain.entity.SaleOrderProductEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 送样表的产品 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-31 19:42:09
 * @since JDK1.8
 */
@Service
public class SaleOrderProductService {

    @Autowired
    private SaleOrderProductDao saleOrderProductDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-31 19:42:09
     */
    public ResponseDTO<PageResultDTO<SaleOrderProductDTO>> queryByPage(SaleOrderProductQueryDTO queryDTO) {
        Page page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<SaleOrderProductEntity> entities = saleOrderProductDao.queryByPage(page, queryDTO);
        List<SaleOrderProductDTO> dtoList = SmartBeanUtil.copyList(entities, SaleOrderProductDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<SaleOrderProductDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-31 19:42:09
     */
    public ResponseDTO<String> add(SaleOrderProductDTO addDTO) {
        SaleOrderProductEntity entity = SmartBeanUtil.copy(addDTO, SaleOrderProductEntity.class);
        saleOrderProductDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-31 19:42:09
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(SaleOrderProductDTO updateDTO) {
        SaleOrderProductEntity entity = SmartBeanUtil.copy(updateDTO, SaleOrderProductEntity.class);
        saleOrderProductDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-31 19:42:09
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        saleOrderProductDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-31 19:42:09
    */
    public ResponseDTO<SaleOrderProductDTO> detail(Long id){
        SaleOrderProductEntity entity = saleOrderProductDao.selectById(id);
        SaleOrderProductDTO dto =  SmartBeanUtil.copy(entity,SaleOrderProductDTO.class);
        return ResponseDTO.succData(dto);
    }
}
